package com.ruoyi.verify.controller;

import com.alibaba.fastjson.JSON;
import com.google.api.client.util.ArrayMap;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.verify.domain.VerifyMemberInfo;
import com.ruoyi.verify.domain.VerifyPlanInfo;
import com.ruoyi.verify.domain.VerifyRequirement;
import com.ruoyi.verify.service.IVerifyPlanInfoService;
import com.ruoyi.verify.service.IVerifyRequirementService;
import com.ruoyi.verify.transform.VerifyPlanMemberInfo;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("requirement")
public class VerifyRequirementController extends BaseController
{

    @Autowired
    private IVerifyRequirementService verifyRequirementService;

    @Autowired
    private IVerifyPlanInfoService verifyPlanInfoService;

    /**
     * 查询${tableComment}
     */
    @GetMapping("get/{requirementid}")
    public R get(@PathVariable("requirementid") Long requirementid)
    {
        VerifyRequirement verifyRequirement = verifyRequirementService.selectVerifyRequirementById(requirementid);
        verifyRequirement.setSupplycategorys(verifyRequirement.getSupplycategory().split(","));
        List<VerifyRequirement> list = new ArrayList<>();
        list.add(verifyRequirement);
        return result(list);
    }

    /**
     * 获取需求列表
     */
    @GetMapping("list")
    public R list(VerifyRequirement verifyRequirement)
    {
        startPage();
        return result(verifyRequirementService.selectVerifyRequirementList(verifyRequirement));
    }


    /**
     * 新增保存【请填写功能名称】
     */
    @PostMapping("save")
    public R addSave(@RequestBody VerifyRequirement verifyRequirement)
    {
        String categorys = StringUtils.join(verifyRequirement.getSupplycategorys(), ",");
//        String category = categorys.substring(1, categorys.length() - 1);
        verifyRequirement.setSupplycategory(categorys);
        return toAjax(verifyRequirementService.insertVerifyRequirement(verifyRequirement));
    }



    /**
     * 修改保存【请填写功能名称】
     */
    @PostMapping("update")
    public R editSave(@RequestBody VerifyRequirement verifyRequirement)
    {
        return toAjax(verifyRequirementService.updateVerifyRequirement(verifyRequirement));
    }

    /**
     * 修改需求状态
     */
    @PostMapping("modifystatus")
    @Transactional(rollbackFor = Exception.class)
    public R modifystatus(@RequestBody Map<String,String> verifyRequirements)
    {
        String verifyrequirementids = verifyRequirements.get("verifyrequirementids");
        String status = verifyRequirements.get("status");
        VerifyRequirement verifyRequirement = new VerifyRequirement();
        for(String verifyrequirementid:verifyrequirementids.split(",")){
            verifyRequirement.setRequirementid(Long.parseLong(verifyrequirementid));
            verifyRequirement.setStatus(Long.parseLong(status));
            toAjax(verifyRequirementService.updateVerifyRequirement(verifyRequirement));
        }
        return R.ok();
    }

    /**
     * 删除${tableComment}
     */
    @PostMapping("remove")
    public R remove(String ids)
    {
        return toAjax(verifyRequirementService.deleteVerifyRequirementByIds(ids));
    }

    /**
     * test
     */
    @GetMapping("tests")
    public void tests(String verifytype,String factorycode){
        VerifyRequirement verifyRequirement = verifyRequirementService.selectIfHaveTemplate(verifytype, factorycode);
        System.out.println(verifyRequirement.getTemplateid());
    }

    /**
     * 根据审核需求生成计划
     */
    @PostMapping("createverifyplanbyrequirement")
//    @Transactional(rollbackFor = Exception.class)
    public R createVerifyPlan(@RequestBody Map<String,List<Long>> verifyrequirementids)
    {
        VerifyPlanInfo parentVerifyPlanInfo = new VerifyPlanInfo();
        parentVerifyPlanInfo.setParentid(-1l);
        if(verifyrequirementids.get("verifyrequirementids")==null || verifyrequirementids.get("verifyrequirementids").equals("")){
            return R.error();
        }
        Long verifyrequirementids1 = verifyrequirementids.get("verifyrequirementids").get(0);
        VerifyRequirement verifyRequirement1 = verifyRequirementService.selectVerifyRequirementById(verifyrequirementids1);
        parentVerifyPlanInfo.setFactorycode(verifyRequirement1.getFactorycode());
        parentVerifyPlanInfo.setVerifytype(verifyRequirement1.getVerifytype());
        VerifyRequirement ifHaveTemplate = verifyRequirementService.selectIfHaveTemplate(verifyRequirement1.getVerifytype(), verifyRequirement1.getFactorycode());

        if(ifHaveTemplate==null){
            return R.error(500,"当前事业部下的此审核类型尚未生成审核模板，请生成模板后再生成计划！");
        }
        parentVerifyPlanInfo.setTemplateid(ifHaveTemplate.getTemplateid());//将模板id绑定到主计划中




        verifyPlanInfoService.insertVerifyPlanInfo(parentVerifyPlanInfo);

        List<VerifyPlanInfo> list1= verifyrequirementids.get("verifyrequirementids").stream().map(vrid -> {
            VerifyPlanInfo verifyPlanInfo = new VerifyPlanInfo();
            VerifyRequirement verifyRequirement = verifyRequirementService.selectVerifyRequirementById(vrid);
            verifyPlanInfo.setVerifyrequirementid(vrid);

            verifyPlanInfo.setVerifytype(verifyRequirement.getVerifytype());
            verifyPlanInfo.setFactorycode(verifyRequirement.getFactorycode());
            verifyPlanInfo.setSuppliername(verifyRequirement.getSuppliername());
            verifyPlanInfo.setSupplycategory(verifyRequirement.getSupplycategory());

            verifyPlanInfo.setParentid(parentVerifyPlanInfo.getPlanid());


            verifyPlanInfoService.insertVerifyPlanInfo(verifyPlanInfo);

            verifyRequirement.setStatus(3l);
            verifyRequirement.setPlanid(parentVerifyPlanInfo.getPlanid());
            verifyRequirementService.updateVerifyRequirement(verifyRequirement);
            return verifyPlanInfo;
        }).collect(Collectors.toList());


        return jacResult(list1);
    }



//    /**
//     * 保存当前计划页内计划
//     * 根据isGroupLeader字段判断是否为组长，若为组长，则将父计划的groupleaderid字段设置为子计划id 2020/10/28
//     */
//    @PostMapping("saveverifyplan")
//    @Transactional(rollbackFor = Exception.class)
//    public R saveVerifyPlanss(@RequestBody Map<String,List<Object>> stringObjectMap){
////        List<VerifyPlanInfo> verifyPlanInfos = JSON.parseObject(JSON.toJSONString()stringObjectMap.get("verifyPlanInfos"),ap.class);
//        List<Object> verifyPlanInfo0 = stringObjectMap.get("verifyPlanInfo");
//        List<VerifyPlanInfo> mainverifyPlanInfo = verifyPlanInfo0.stream().map(vpi -> JSON.parseObject(JSON.toJSONString(vpi), VerifyPlanInfo.class)).collect(Collectors.toList());
//        VerifyPlanInfo verifyPlanInfo = mainverifyPlanInfo.get(0);
//
//        List<Object> verifyPlanInfos0 = stringObjectMap.get("verifyPlanInfos");
//        List<VerifyPlanInfo> verifyPlanInfos = verifyPlanInfos0.stream().map(vpi -> JSON.parseObject(JSON.toJSONString(vpi), VerifyPlanInfo.class)
//        ).collect(Collectors.toList());
//        verifyPlanInfos.stream().forEach(vpi -> {
//            String categorys = StringUtils.join(vpi.getSupplycategorys());
////            String category = categorys.substring(1,categorys.length()-1);
//            vpi.setSupplycategory(categorys);
//        });
//
//        List<Object> verifyplanmembermoudles0 = stringObjectMap.get("verifyplanmembermoudle");
//        List<VerifyPlanMemberInfo.innerVerify> verifyplanmembermoudles = verifyplanmembermoudles0.stream().map(vpmm -> JSON.parseObject(JSON.toJSONString(vpmm), VerifyPlanMemberInfo.innerVerify.class)
//        ).collect(Collectors.toList());
//
//        verifyplanmembermoudles.stream().filter(vpmm -> vpmm.getIsgroupleader().equals("1")).forEach(v -> verifyPlanInfo.setGroupleaderid(v.getMemberid()));
//
//
//        VerifyPlanMemberInfo verifyPlanMemberInfo = new VerifyPlanMemberInfo();
//        verifyPlanMemberInfo.setVerifyPlanInfo(verifyPlanInfo);
//        verifyPlanMemberInfo.setVerifyPlanInfos(verifyPlanInfos);
//        verifyPlanMemberInfo.setVerifyplanmembermoudle(verifyplanmembermoudles);
//
//        verifyPlanInfoService.saveCurrentPlan(verifyPlanMemberInfo);
//
//        Map<String,Object> map = new ArrayMap<>();
//        map.put("planid",verifyPlanInfo.getPlanid());
//
//        return R.ok(map);
//    }



}
